package com.rxnetwork.sample.mvp.toolbar

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout

import com.rxnetwork.sample.R
import com.rxnetwork.sample.utils.StatusBarUtil

import java.lang.reflect.Field

/**
 * by xiaocode
 * 带标题栏的fragment类
 */
abstract class BaseTopBarFragment : Fragment() {

    var topBarLayout: TopbarLayout? = null
        private set

    /**
     * 便于设置全局崩溃，网络错误，空白页

     * @param contentLayout
     */
    var contentLayout: FrameLayout? = null

    private //处理android 4.4以下会崩溃
    val contentViewLayout: View
        get() {
            val view = View.inflate(activity, R.layout.activity_top_bar, null)
            topBarLayout = view.findViewById(R.id.top_bar_layout) as TopbarLayout
            topBarLayout?.setTitle(topTitle)
            try {
                StatusBarUtil.setColor(activity, if (topBarLayout?.topbarBgColor as Int > 0)
                    topBarLayout?.topbarBgColor as Int
                else
                    resources.getColor(android.R.color.white), 0)
            } catch (e: Exception) {
            }

            contentLayout = view.findViewById(R.id.content_layout) as FrameLayout
            if (contentViewResId > 0) {
                View.inflate(activity, contentViewResId, contentLayout)
            }
            return view
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (contentViewResId > 0) {
            activity.setContentView(contentViewLayout)
        }
        try {
            setTopBarLayoutColor(resources.getColor(android.R.color.white))
        } catch (e: Exception) {
        }

        initPresenter()
        initViewsAndEvents()
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var view: View? = super.onCreateView(inflater, container, savedInstanceState)
        if (contentViewResId > 0) {
            if (inflater != null) {
                view = inflater?.inflate(contentViewResId, container, false)
            }
        }
        return view as View
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewsAndEvents()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        try {
            val childFragmentManager = Fragment::class.java.getDeclaredField("mChildFragmentManager")
            childFragmentManager.isAccessible = true
            childFragmentManager.set(this, null)
        } catch (e: NoSuchFieldException) {
            throw RuntimeException(e)
        } catch (e: IllegalAccessException) {
            throw RuntimeException(e)
        }

    }

    override fun onDetach() {
        super.onDetach()
        // for bug ---> java.lang.IllegalStateException: Activity has been destroyed
        try {
            val childFragmentManager = Fragment::class.java.getDeclaredField("mChildFragmentManager")
            childFragmentManager.isAccessible = true
            childFragmentManager.set(this, null)

        } catch (e: NoSuchFieldException) {
            throw RuntimeException(e)
        } catch (e: IllegalAccessException) {
            throw RuntimeException(e)
        }

    }

    fun setTopBarLayoutColor(color: Int) {
        topBarLayout?.topbarBgColor = color
        StatusBarUtil.setColor(activity, color, 0)
    }

    protected abstract fun initPresenter()

    protected abstract fun initViewsAndEvents()

    protected abstract val contentViewResId: Int

    protected abstract val topTitle: CharSequence

}
